package com.itheima.admin.controller;

import com.itheima.admin.dto.AdChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.core.controller.AbstractCoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
@Slf4j
public class AdChannelController extends AbstractCoreController<AdChannel> {


    @Autowired
    private AdChannelService adChannelService;

    @Autowired
    public  AdChannelController (AdChannelService adChannelService){
        super(adChannelService);
    }

    /**
     * 频道分页查询
     *
     * @param dto
     * @return
     */

    @ApiOperation("频道分页搜索")
    @PostMapping("/list")
    public PageResultVo list(@RequestBody AdChannelPageRequestDto dto) {
        PageResultVo pageResultVo = adChannelService.pageList(dto);
        return pageResultVo;
    }


    /**
     * 新增频道
     *
     * @param adChannel
     * @return
     */
    @ApiOperation("新增频道")
    @PostMapping("/save")
    @Override
    public ResultVo insert(@RequestBody AdChannel adChannel) {
        if (null ==adChannel){
            throw new LeadNewsException("对象为空");
        }
        Boolean aBoolean = adChannelService.add(adChannel);
        if (aBoolean) {
            return ResultVo.ok("success");
        }
        return ResultVo.error();
    }






}
